Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
How to "catch" console.error()? (SO title)

I included a lib that uses console.error() instead of throw.

Because of this, my try...catch doesn't work. And because it's a 3rd party lib, I can't change their code.

Is there a (preferably elegant) workaround to "catch" a console.error()?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You cannot catch something that doesn't throw any errors. It looks like a crutch but you can override your console.error method in the code below. But don't forget that after you will have overriden console.error that throws errors!!!

console.errorWithoutExceptions = console.error;
console.error = (...messages) => {
    console.errorWithoutExceptions(...messages); 
    throw new Error(messages[0]);
}

// now you can use 
try {
   // your code
   console.error('test error');
} catch(e) {
   // process you error with message 'test error' here
}

// don't forget to restore previous console.error after using
console.error = console.errorWithoutExceptions;

about 4 years ago · Juan Pablo Isaza Report

0

You can try and manipulate the global console object to your requirement. But that's messy and might generate additional errors.

More here : Console | Node.js Docs

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!